<?php

namespace hunma\basic;

use support\Response;

class ApiResponse
{
    /**
     * 成功响应
     *
     * @param mixed $data
     * @param string $message
     * @param int $code
     * @return Response
     */
    public static function success($data = null, string $message = 'success', int $code = 200): Response
    {
        return new Response($code, [
            'Content-Type' => 'application/json',
        ], json_encode([
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ],JSON_UNESCAPED_UNICODE));
    }

    /**
     * 错误响应
     *
     * @param string $message
     * @param int $code
     * @param mixed $data
     * @return Response
     */
    public static function error(string $message = 'error', int $code = 400,$data = null): Response
    {
        return new Response($code, [
            'Content-Type' => 'application/json',
        ], json_encode([
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ],JSON_UNESCAPED_UNICODE));
    }

    public static function rySuccess($data = null, string $message = 'success', int $code = 200): Response
    {

        $resData = [
            'code' => $code,
            'msg' => $message,
        ];

        foreach ($data as $k=>$v){
            $resData[$k] = $v;
        }

        return new Response($code, [
            'Content-Type' => 'application/json',
        ], json_encode($resData,JSON_UNESCAPED_UNICODE));
    }

    public static function ryError(string $message = 'error', int $code = 500,$data = null): Response
    {
        $resData = [
            'code' => $code,
            'msg' => $message,
        ];

        if(!empty($data)){
            foreach ($data as $k=>$v){
                $resData[$k] = $v;
            }
        }

        return new Response(200, [
            'Content-Type' => 'application/json',
        ], json_encode($resData,JSON_UNESCAPED_UNICODE));
    }

    public static function ValidationError(string $message = 'error', int $code = 400): Response
    {
        $resData = [
            'code' => $code,
            'msg' => $message,
        ];
        return new Response(200, [], json_encode($resData,JSON_UNESCAPED_UNICODE));
    }
}